#!/bin/sh
#
#
#   Copyright (c) International Business Machines  Corp., 2001
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#
#  FILE   : stress_floppy
#
#  PURPOSE : Tests the reading/writing/formatting on a floppy drive
#
#  HISTORY:
#    06/01 Robbie Williamson (robbiew@us.ibm.com)
#     -Ported
#
#
#---------------------------------------------------------------------------

TCdat=${TCdat:-$LTP_DATAROOT}

TCID="io_floppy"
TST_TOTAL=7
. test.sh

setup()
{
	tst_tmpdir

	tst_require_cmds fdformat tar dump cpio dd mkfs mkdosfs

	TCtmp=$(pwd)
}

cleanup()
{
	tst_rmdir
}


test_for_device()
{
	grep fd /proc/devices
	if [ $? = 0 ]; then
		num_device=$(ls /dev | grep fd0 | wc -l)
		if [ $num_device = 0 ]; then
			tst_brkm TCONF "No floppy diskette drive available!"
		else
			tst_resm TINFO "Floppy diskette drive fd0 available."
		fi
	else
		tst_brkm TCONF "No floppy diskette drive available!"
	fi
}

test_format()
{
	tst_resm TINFO "Testing format..."
	fdformat  /dev/fd0
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "Format failed!"
	else
		tst_resm TPASS "Format successful."
	fi
}

test_tar()
{
	for the_file in 1K_file 10K_file 100K_file 1000K_file
	do
		tst_resm TINFO "Testing $the_file tar..."
		tar -C $TCdat/dumpdir -cvf /dev/fd0 $the_file
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file Tar write failed!"
			return
		else
			tst_resm TINFO "$the_file Tar write passed."
		fi
		tar -xvf /dev/fd0
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file Tar read failed!"
			return
		else
			tst_resm TINFO "$the_file Tar read passed."
		fi
		diff $TCdat/dumpdir/$the_file $the_file >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "Diff of the $the_file tar files failed!"
			return
		else
			tst_resm TINFO "Diff of the $the_file tar files passed."
		fi
		rm -f $the_file
	done
	tst_resm TPASS "test_tar: PASS."
}

test_dump()
{
	tst_resm TINFO "Testing dump/restore..."
	cp -r $TCdat/dumpdir ./
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "Could not create dumpdir directory in $TCtmp"
		return
	fi

	dump -f /dev/fd0 dumpdir
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "$the_file dump write failed!"
		return
	else
		tst_resm TINFO "$the_file dump write passed."
	fi

	rm -rf dumpdir
	cd /

	restore -v -r -f /dev/fd0 2>/dev/null
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "$the_file restore read failed!"
		return
	else
		tst_resm TINFO "$the_file restore read passed."
	fi

	cd -

	for the_file in 1K_file 10K_file 100K_file
	do
		diff dumpdir/$the_file /$TCdat/dumpdir/$the_file >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			tst_resm TFAIL \
				 "Diff of the $the_file backup files failed!"
			return
		else
			tst_resm TINFO \
				 "Diff of the $the_file backup files passed."
		fi
	done
	tst_resm TPASS "test_dump: PASS."
	rm -rf dumpdir
	rm -f restoresymtable
}

test_cpio()
{
	for the_file in 1K_file 10K_file 100K_file 1000K_file
	do
		tst_resm TINFO "Testing $the_file cpio..."
		cd $TCdat/dumpdir
		echo $the_file | cpio -o > /dev/fd0
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file cpio write failed!"
			return
		else
			tst_resm TINFO "$the_file cpio write passed."
		fi
		cd $TCtmp
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "Could not change to $TCtmp directory!"
			return
		fi
		cpio -i < /dev/fd0
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file cpio read failed!"
			return
		else
			tst_resm TINFO "$the_file cpio read passed."
		fi
		diff $TCdat/dumpdir/$the_file $the_file >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			tst_resm TFAIL \
				 "Diff of the $the_file cpio files failed!"
			return
		else
			tst_resm TINFO \
				 "Diff of the $the_file cpio files passed."
		fi
		rm -f $the_file
	done
	tst_resm TPASS "test_cpio: PASS."
}

test_dd()
{
	for the_file in dd_file
	do
		tst_resm TINFO "Testing $the_file dd..."
		dd if=$TCdat/$the_file of=/dev/fd0 ibs=1b obs=90b conv=sync
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file dd write failed!"
			return
		else
			tst_resm TINFO "$the_file dd write passed."
		fi
		dd if=/dev/fd0 of=$the_file ibs=90b obs=1b conv=sync
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "$the_file dd read failed!"
			return
		else
			tst_resm TINFO "$the_file dd read passed."
		fi
		diff $TCdat/$the_file $the_file >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "Diff of the $the_file dd files failed!"
			return
		else
			tst_resm TINFO "Diff of the $the_file dd files passed."
		fi
		rm -f $the_file
	done
	tst_resm TPASS "test_dd: PASS."
}

test_linuxformat()
{
	tst_resm TINFO "Testing mkdosfs...."
	mkfs -v /dev/fd0
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "Linux (ext2) format failed!"
	else
		tst_resm TPASS "Linux (ext2) successful."
	fi
}

test_dosformat()
{
	tst_resm TINFO "Testing mkdosfs...."
	mkdosfs -v /dev/fd0
	if [ $? -ne 0 ]; then
		tst_resm TFAIL "Dosformat failed!"
	else
		tst_resm TPASS "Dosformat successful."
	fi
}

setup

TST_CLEANUP=cleanup

test_for_device

test_format
test_tar
test_dump
test_cpio
test_dd
test_linuxformat
test_dosformat

tst_exit
